﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Drawing;

public partial class Bidder_GoodDetail : System.Web.UI.Page
{
    Int64 GoodsId;
    string strSql;
    public string straa;
    public string strbb;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))//是否为合法用户
        {
            if (!IsPostBack)
            {
                straa = "none";
                strbb = "none";
                if (Request.QueryString["GoodsId"] == null)
                {
                    Response.Write("<script>window.alter('没有要添加的商品或页面过期，请重新点击！！')</script>");
                }
                else
                {
                    if (HZ.Fun.IsInt(Request.QueryString["GoodsId"]))
                    {
                        GoodsId = Convert.ToInt64(Request.QueryString["GoodsId"]);
                    }
                    else
                    {
                        string _goodsid = MsgBox.JieMi_Decrypt(Request.QueryString["GoodsId"].ToString());
                        if (HZ.Fun.IsInt(_goodsid))
                        {
                            GoodsId = Convert.ToInt64(_goodsid);
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("没有要添加的商品或页面过期，请重新点击！！");
                            Response.End();
                        }
                    }

                 
                    // GoodsId = Convert.ToInt64(SysFun.IsSQL(MsgBox.JieMi_Decrypt(Request.QueryString["GoodsId"].ToString())));


                    //********   08.11   start   增加判断通过url传参数过来的goodid是否是用户的代码。防止安全问题


                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

                    Db dbone = new Db();
                    string str = "select * from Goods  where AccountId_TJR = '"
                        + _Account.AccountId
                        + "'  and GoodsId = '"
                        + GoodsId
                        + "'";
                    bool isRight = false;
                    System.Data.IDataReader dr1;
                    dr1 = dbone.DataReader(str);
                    while (dr1.Read())
                    {
                        isRight = true; break;//该goodsid是该用户的商品

                    }

                    dr1.Close();
                    dr1.Dispose();
                    dbone.Close();
                    if (isRight)
                    {
                        //****  end
                        Db db = new Db();
                        try
                        {
                            System.Data.IDataReader dr;
                            labNoPass.Visible = false;
                            labNoPass1.Visible = false;
                            butUpdate.Visible = false;
                            strSql = "select * from v_product_goods where GoodsId=" + GoodsId.ToString();
                            dr = db.DataReader(strSql);
                            while (dr.Read())
                            {
                                rbtnYb.SelectedValue = dr["memo1"].ToString();
                                txtProductNameSM.Text = dr["ProductNameSM"].ToString();
                                txtAccountId_TJR.Text = dr["AccountId_TJR"].ToString();
                                txtAccountId_TJR.Visible = false;
                                labProductCode.Text = dr["productid"].ToString();
                                labProductId.Text = dr["ProductId"].ToString();
                                labProductId.Visible = false;
                                labProductName.Text = dr["productname"].ToString();
                                string _strMedicinemodel = dr["medicinemodel"].ToString();
                                string _strOutlookc = dr["outlookc"].ToString();
                                labMedicinemodel.Text = _strMedicinemodel;
                                labOutlookc.Text = _strOutlookc;
                                if (_strMedicinemodel == "大输液" && _strOutlookc.IndexOf("玻瓶") == -1)
                                {
                                    Label4.Text = Label2.Text + "(大容量制剂（治疗用大输液）未标明包材的为玻瓶。)";
                                    Label4.BackColor = Color.Yellow; ;
                                }
                                //labOutlookc.Text = dr["outlookc"].ToString();
                                //labMedicinemodel.Text = dr["medicinemodel"].ToString();
                                txtunit.Text = dr["g_unit"].ToString();
                                txtCompany.Text = dr["companyName"].ToString();
                                txtFactor0.Text = dr["Factor"].ToString();
                                packUnit.Text = dr["PackUnit"].ToString();
                                //txtFactor.Enabled = false;
                                txtGoodsId.Text = dr["goodsid"].ToString();
                                txtGoodsId.Visible = false;
                                txtGoodsInstruction.Text = dr["GoodsInstruction"].ToString();
                                txtGoodsName.Text = dr["goodsname"].ToString();
                                txtImportCode.Text = dr["ImportCode"].ToString();
                                txtImportRegistDate.Text = dr["ImportRegistDate"].ToString();
                                if (SysFun.ToTrim(dr["ImportRegistDate"]) == "1900-1-1 0:00:00")
                                {
                                    txtImportRegistDate.Text = "";
                                }
                                txtIsSalerId.Text = dr["AccountId_SC"].ToString();
                                txtLicenseNumber.Text = dr["licensenumber"].ToString();
                                txtLicenseNumberDate.Text = dr["licensenumberdate"].ToString();
                                if (SysFun.ToTrim(dr["licensenumberdate"]) == "1900-1-1 0:00:00")
                                {
                                    txtLicenseNumberDate.Text = "";
                                }
                                txtMaterial.Text = dr["Material"].ToString();
                                txtPackUnit.Text = dr["PackUnit"].ToString();
                                txtPackUnit.Visible = false;
                                rdoQualityLevels.SelectedValue = SysFun.ToTrim(dr["QualityLevels"]);
                                if (rdoQualityLevels.SelectedValue == "0") lableshow.Text = "第一质量层次： 专利保护期内的化合物专利药品和药物组合物专利药品；获得国家科技进步奖的药品";
                                if (rdoQualityLevels.SelectedValue == "1") lableshow.Text = "第二质量层次： 发改委公布的单独定价药品和优质优价中成药";
                                if (rdoQualityLevels.SelectedValue == "2") lableshow.Text = "第三质量层次： 专利保护期内的工艺流程发明专利药品（含剂型专利药品）；过保护期的（化合物、药物组合物）专利药品；进口药品；获得美国ＦＤＡ认证或欧盟ＣＥ、ＣＧＭＰ认证的药品；质量标准起草单位的药品（含原研药品和首仿国外药品）";
                                if (rdoQualityLevels.SelectedValue == "3") lableshow.Text = "第四质量层次： 通过GMP认证的其他药品";
                                txtRetailPrice.Text = dr["RetailPrice"].ToString();
                                labNoPass.Text = dr["nopass"].ToString();

                                if (SysFun.ToTrim(dr["IsImport"]) != "")
                                {
                                    chkIsImport.Checked = Convert.ToBoolean(dr["IsImport"].ToString());
                                }
                                else
                                {
                                    chkIsImport.Checked = false;
                                }
                                if (chkIsImport.Checked == false)
                                {
                                    straa = "none";
                                }
                                else
                                {
                                    straa = "block";
                                }

                                if (dr["IsSurveyReport"].ToString() != "")
                                {
                                    chkIsSurveyReport.Checked = Convert.ToBoolean(dr["IsSurveyReport"].ToString());
                                }
                                else
                                {
                                    chkIsSurveyReport.Checked = false;
                                }
                                if (chkIsSurveyReport.Checked == false)
                                {
                                    strbb = "none";
                                }
                                else
                                {
                                    strbb = "block";
                                    txtSurveyReportDate.Text = dr["SurveyReportDate"].ToString();
                                    if (SysFun.ToTrim(dr["SurveyReportDate"]) == "1900-1-1 0:00:00")
                                    {
                                        txtSurveyReportDate.Text = "";
                                    }
                                    //else
                                    //{
                                    //    if (Convert.ToDateTime(dr["SurveyReportDate"]) <= System.DateTime.Now)
                                    //    {
                                    //        LabelYaoPinJYBG.Text = "过期";
                                    //    }
                                    //}
                                    txtSurveyReportId.Text = dr["SurveyReportId"].ToString();
                                }

                                this.txtjz.Text = dr["jzdate"].ToString();
                                txtIsalerId_dL.Text = dr["AccountId_Agent"].ToString();
                                txtIsalerId_dL.Visible = false;
                                txtIsSalerId.Text = dr["AccountId_SC"].ToString();
                                txtIsSalerId.Visible = false;
                                txtCompany_dl.Text = dr["companyName_Agent"].ToString();
                                txtMemo4.Text = SysFun.ToTrim(dr["memo4"]);
                                txtMemo5.Text = dr["memo5"].ToString();

                                this.txtGmpCode.Text = dr["GmpCode"].ToString();
                                this.txtGmpValDate.Text = dr["GmpValDate"].ToString();


                                hidCheckStatus.Value = dr["CheckStatus"].ToString();
                                ViewState["status"] = dr["CheckStatus"].ToString();
                                hidShenHeTime.Value = dr["ShenHeTime"].ToString();

                                if (SysFun.ToInt(dr["ProductAddStatus"].ToString()) == 1)
                                {
                                    BtnSave.Visible = true;
                                    BtnTJ.Visible = true;
                                }
                                else
                                {
                                    BtnSave.Visible = false;
                                    BtnTJ.Visible = true;
                                }

                                //08.14  增加如果是老数据，就不让修改，不管是否审核  ischange = 1 表示是老数据
                                //start

                                if (dr["ischange"].ToString().ToLower() == "true") // 老数据处理
                                {

                                    rdoQualityLevels.Enabled = false;
                                    txtMemo5.ReadOnly = true; //禁止修改质量层次
                                    this.Label3.Text = "<input id=\"Button3\" type=\"button\" value=\"选择一级代理商\"  class=\"button_100\" onclick ='Sel_dl()' />";


                                    //**** 08.14  
                                    if (dr["CheckStatus"].ToString() != "1")//审核未通过
                                    {
                                        labNoPass1.Visible = true;
                                        labNoPass.Visible = true;
                                    }
                                    else
                                    {
                                        this.Label3.Visible = false;
                                        txtImportCode.ReadOnly = true;
                                        txtLicenseNumber.ReadOnly = true;
                                        //txtLicenseNumberDate.Enabled = true;
                                        txtLicenseNumberDate.ReadOnly = true;
                                        txtImportRegistDate.ReadOnly = true;
                                        txtGoodsName.ReadOnly = true;
                                        //txtFactor.ReadOnly = true;
                                        txtMaterial.ReadOnly = true;
                                        //txtPackUnit.ReadOnly = true;
                                        txtRetailPrice.ReadOnly = true;
                                        rdoQualityLevels.Enabled = false;
                                        chkIsImport.Enabled = false;
                                        chkIsSurveyReport.Enabled = false;
                                        txtSurveyReportId.ReadOnly = true;
                                        txtSurveyReportDate.ReadOnly = true;
                                        txtGoodsInstruction.ReadOnly = true;
                                        txtjz.ReadOnly = true;
                                        txtLicenseNumberDate.ReadOnly = true;
                                        BtnSave.Visible = false;
                                        BtnCancel.Visible = true;
                                        BtnTJ.Visible = false;
                                        LBSave.Visible = false;
                                        LBTJ.Visible = false;
                                        txtMemo5.ReadOnly = true;
                                        txtMemo4.ReadOnly = true;
                                        txtLicenseNumberDate.ReadOnly = true;
                                        txtProductNameSM.ReadOnly = true;
                                    }

                                }  //end
                                else
                                    if (dr["CheckStatus"].ToString() == "1")//审核通过
                                    {

                                        this.Label3.Visible = false;
                                        txtImportCode.ReadOnly = true;
                                        txtLicenseNumber.ReadOnly = true;
                                        //txtLicenseNumberDate.Enabled = true;
                                        txtLicenseNumberDate.ReadOnly = true;
                                        txtImportRegistDate.ReadOnly = true;
                                        txtGoodsName.ReadOnly = true;
                                        //txtFactor.ReadOnly = true;
                                        txtMaterial.ReadOnly = true;
                                        //txtPackUnit.ReadOnly = true;
                                        txtRetailPrice.ReadOnly = true;
                                        rdoQualityLevels.Enabled = false;
                                        chkIsImport.Enabled = false;
                                        chkIsSurveyReport.Enabled = false;
                                        txtSurveyReportId.ReadOnly = true;
                                        txtSurveyReportDate.ReadOnly = true;
                                        txtGoodsInstruction.ReadOnly = true;
                                        butUpdate.Visible = false;
                                        BtnSave.Visible = false;
                                        BtnCancel.Visible = true;
                                        BtnTJ.Visible = false;
                                        LBSave.Visible = false;
                                        LBTJ.Visible = false;
                                        txtMemo5.ReadOnly = true;
                                        txtMemo4.ReadOnly = true;
                                        txtLicenseNumberDate.ReadOnly = true;
                                        txtProductNameSM.ReadOnly = true;
                                    }
                                    else if (dr["CheckStatus"].ToString() == "2")
                                    {
                                        //add can not edit 2008.8.6  by wushuang
                                        //增加只要是审核过的(包括通过与不通过的都不能修改)
                                        /* 
                                       this.Label3.Visible = false;
                                       txtImportCode.ReadOnly = true;
                                       txtLicenseNumber.ReadOnly = true;
                                       //txtLicenseNumberDate.Enabled = true;
                                       txtLicenseNumberDate.ReadOnly = true;
                                       txtImportRegistDate.ReadOnly = true;
                                       txtGoodsName.ReadOnly = true;
                                       //txtFactor.ReadOnly = true;
                                       txtMaterial.ReadOnly = true;
                                       //txtPackUnit.ReadOnly = true;
                                       txtRetailPrice.ReadOnly = true;
                                       rdoQualityLevels.Enabled = false;
                                       chkIsImport.Enabled = false;
                                       chkIsSurveyReport.Enabled = false;
                                       txtSurveyReportId.ReadOnly = true;
                                       txtSurveyReportDate.ReadOnly = true;
                                       txtGoodsInstruction.ReadOnly = true;
                                       BtnTJ.Visible = false;
                                       LBSave.Visible = false;
                                       LBTJ.Visible = false;
                                       txtMemo5.ReadOnly = true;
                                       txtMemo4.ReadOnly = true;
                                       txtLicenseNumberDate.ReadOnly = true;
                                       //add over
                                         */
                                        //禁止修改质量层次
                                       // rdoQualityLevels.Enabled = false;
                                       // txtMemo5.ReadOnly = true;
                                        BtnSave.Visible = false;
                                        BtnCancel.Visible = true;
                                        labNoPass1.Visible = true;
                                        labNoPass.Visible = true;
                                        BtnTJ.Visible = true;  //原为true 贾鹏要求：该按钮不显示！2008/11/12 周传雄
                                        butUpdate.Visible = true;
                                        this.Label3.Text = "<input id=\"Button3\" type=\"button\" value=\"选择一级代理商\"  class=\"button_bg6\" style='width: 89px'  onclick ='Sel_dl()' />";

                                    }
                                    else
                                    {
                                        this.Label3.Text = "<input id=\"Button3\" type=\"button\" value=\"选择一级代理商\"  class=\"button_bg6\" style='width: 89px'  onclick ='Sel_dl()' />";

                                    }

                                txtsh_discrible.Text = SysFun.ToTrim(dr["sh_discrible"].ToString());

                            }

                            dr.Close();
                            dr.Dispose();
                        }
                        catch { throw; }
                        finally
                        {
                            db.Close();
                        }
                    }
                    else
                    {
                        MsgBox.Aert("非法操作！", "GoodsList.aspx");
                        return;
                    }

                }
            }
        }
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        //add can not edit 2008.8.6  by wushuang
        //增加只要是审核过的(包括通过与不通过的都不能修改)
        /*
      
        if (ViewState["status"].ToString().Equals("2"))
        {
            Response.Write("<script language='javascript'>alert('状态已被审核为未通过，不能修改！');</script>");
            return;
        } */
        if (ViewState["status"].ToString().Equals("1"))
        {
            Response.Write("<script language='javascript'>alert('数据已审核通过，不能修改！');</script>");
            return;
        }
        if (CompanySave("0") == true)
        {
            Response.Write("<script>alert('保存成功！！！');window.close();</script>");
            Response.Write("<script>window.opener.location.reload();window.close();</script>");
        }



    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }

    public bool CompanySave(string strMemo3)
    {
        if (ViewState["status"].ToString().Equals("3"))
        {
            Response.Write("<script language='javascript'>alert('状态为原始状态，不能修改！');</script>");
            return false;
        }

        Db db = new Db(false);
        HZ.MBSM.BLL.Goods userGoodsUpdate = new HZ.MBSM.BLL.Goods();
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        if (goods != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {


                int rowCun = 0;
                Db db1 = new Db();
                System.Data.IDataReader dr;
                strSql = "select count(GOODSID) as cun from dbo.GOODS where   PRODUCTID='" + SysFun.ToTrim(labProductId.Text) + "'  AND  FACTOR= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor0.Text)) + "' AND ACCOUNTID_SC='" + SysFun.ToTrim(SysFun.ToLong(txtIsSalerId.Text.ToString())) + "' and GoodsId<>" + SysFun.ToTrim(txtGoodsId.Text);
                dr = db1.DataReader(strSql);
                if (dr.Read())
                {
                    rowCun = SysFun.ToInt(dr["cun"]);
                }
                dr.Close();
                dr.Dispose();
                db1.Close();
                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');</script>");
                    return false;
                }



                else if (SysFun.ToTrim(txtMemo5.Text) == "" && Convert.ToInt32(SysFun.ToTrim(txtGoodsId.Text)) > 86360 && strMemo3 != "2")//如果是9月26日下午三点以前的商品不验证证书号,如果是审核未通过的
                {
                    Response.Write("<script>alert('药品质量层次对应的证书编号必须填写！！！');</script>");
                    return false;
                }
                else
                {
                    string IsSalerId;
                    if (chkIsImport.Checked == true)
                    {
                        if (SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]) == "")
                        {
                            IsSalerId = SysFun.ToTrim(txtIsalerId_dL.Text.ToString());
                        }
                        else
                        {
                            IsSalerId = SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]);
                        }

                        goods.ImportCode = SysFun.ToTrim(txtImportCode.Text);
                        goods.AccountId_Agent = SysFun.ToLong(IsSalerId);
                        if (txtImportRegistDate.Text != "")
                            goods.ImportRegistDate = Convert.ToDateTime(txtImportRegistDate.Text.ToString());
                        else
                            goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");
                    }
                    goods.memo1 = rbtnYb.SelectedIndex;
                    goods.GoodsId = Convert.ToInt64(txtGoodsId.Text);
                    goods.AccountId_SC = SysFun.ToLong(txtIsSalerId.Text.ToString());
                    goods.AccountId_TJR = SysFun.ToLong(txtAccountId_TJR.Text);
                    goods.CheckStatus = hidCheckStatus.Value;
                    if (hidShenHeTime.Value != "")
                        goods.shenhetime = Convert.ToDateTime(hidShenHeTime.Value);
                    else
                        goods.shenhetime = Convert.ToDateTime("1900-1-1");

                    goods.Factor = Convert.ToInt32(txtFactor0.Text.ToString());
                    goods.G_unit = txtunit.Text.Trim();//制剂单位
                    goods.PackUnit = packUnit.Text.Trim();//包装单位；
                    goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                    goods.GoodsName = txtGoodsName.Text.ToString();

                    goods.IsImport = chkIsImport.Checked;
                    goods.IsSurveyReport = chkIsSurveyReport.Checked;
                    goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                    if (txtLicenseNumberDate.Text != "")
                        goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                    else
                        goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");

                    goods.Material = txtMaterial.Text.ToString();
                    goods.Pack = ""; 
                    if (labProductId.Text != "")
                        goods.ProductId = Convert.ToInt64(labProductId.Text.ToString());
                    goods.QualityLevels = SysFun.ToTrim(rdoQualityLevels.SelectedValue);
                    if (txtRetailPrice.Text != "")
                        goods.RetailPrice = Convert.ToDouble(txtRetailPrice.Text.ToString());
                    if (txtSurveyReportDate.Text != "")
                        goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                    else
                        goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                    goods.SurveyReportId = txtSurveyReportId.Text.ToString();
                    goods.memo3 = strMemo3.ToString();
                    goods.memo4 = SysFun.ToTrim(txtMemo4.Text);
                    goods.memo5 = SysFun.ToTrim(txtMemo5.Text);
                    goods.ImgUrl = "";
                    goods.NoPass = labNoPass.Text;
                    goods.ProductNameSM = txtProductNameSM.Text;
                    goods.GmpCode = this.txtGmpCode.Text.Trim();
                    if (txtGmpValDate.Text != "")
                    {
                        goods.GmpValDate = Convert.ToDateTime(this.txtGmpValDate.Text.Trim());

                    }
                    else
                    {

                        goods.GmpValDate = Convert.ToDateTime("1900-1-1");
                    }
                    goods.Jzdate = this.txtjz.Text.ToString();

                    if (userGoodsUpdate.UpdateByCompany(goods) == true)
                    {
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "goods", "userGoodsUpdate.UpdateByCompany(goods)" + goods.GoodsId.ToString(), "../Bidder/GoodsList.aspx");

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        else
        {
            Response.Write("<script language=JavaScript>alert('您无权修改商品！！！');</script>");
            return false;
        }
        return false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {    //add can not edit 2008.8.6  by wushuang
        //增加只要是审核过的(包括通过与不通过的都不能修改)
        if (ViewState["status"].ToString().Equals("1"))
        {
            Response.Write("<script language='javascript'>alert('数据已审核通过，不能修改！');</script>");
            return;
        }
        /*
        if (ViewState["status"].ToString().Equals("2"))
        {
            Response.Write("<script language='javascript'>alert('状态已被审核为未通过，不能修改！');</script>");
            return;
        }
         */
        if (CompanySave("1") == true)
            Response.Write("<script>alert('提交成功！！！');</script>");
        Response.Write("<script>window.opener.location.reload();window.close();</script>");


    }
    protected void butUpdate_Click(object sender, EventArgs e)
    {    //add can not edit 2008.8.6  by wushuang
        //增加只要是审核过的(包括通过与不通过的都不能修改)
        if (ViewState["status"].ToString().Equals("1"))
        {
            Response.Write("<script language='javascript'>alert('数据已审核通过，不能修改！');</script>");
            return;
        }
        /*
        if (ViewState["status"].ToString().Equals("2"))
        {
            Response.Write("<script language='javascript'>alert('状态已被审核为未通过，不能修改！');</script>");
            return;
        }
         */
        if (CompanySave("2") == true)
            Response.Write("<script>alert('保存成功！！！');</script>");
        Response.Write("<script>window.opener.location.reload();window.close();</script>");
    }
}
